package com.example.tobebigfe

import android.content.Intent
import android.hardware.SensorEvent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import com.example.tobebigfe.jsbridge.WebActivity
import com.example.tobebigfe.utils.ShakeSensor
import com.example.tobebigfe.web.DevSettingsActivity
import com.example.tobebigfe.web.WebManager

class MainActivity : WebActivity(), ShakeSensor.OnShakeListener {

    private lateinit var shakeSensor: ShakeSensor

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (BuildConfig.DEBUG) {
            shakeSensor = ShakeSensor(this)
            shakeSensor.shakeListener = this
            shakeSensor.register()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (BuildConfig.DEBUG) {
            shakeSensor.unregister()
        }
    }

    override fun getLoadUrl(): String {
        return WebManager.getWebUrl("home", "index.html")
    }

    override fun onShakeComplete(event: SensorEvent) {
        // 摇一摇显示开发环境
        AlertDialog.Builder(this)
            .setTitle("打开开发环境?")
            .setItems(arrayOf("取消", "确定")) { _, index ->
                if (index == 1) {
                    gotoDevSettings()
                }
            }
            .create()
            .show()
    }

    private fun gotoDevSettings() {
        startActivity(Intent(this, DevSettingsActivity::class.java))
    }
}
